// components/workTaskItems/index.js
const api = require('../../utils/request.js')
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    workItem: {
      type: Object,
      value: null
    },
    workIndex: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    createOrder() {
      const that = this
      return new Promise((resolve, reject) => {
        api._post('/Pay/Order/Create', {
          itemID: this.data.workItem.ID,
          orderType: 3
        }, {
          loadingText: '正在生成订单'
        }).then(res => {
          if (res.Code === 100000) {
            if (res.Data.Price === 0){
              wx.showToast({
                title: '支付成功',
              })
              that.setData({
                'workItem.State': 2
              })
            }
            else{
              this.payOrder(res.Data)
            }
            resolve(res.Data)
          }
        })
      })
    },

    payOrder(object) {
      const that = this
      wx.requestPayment({
        timeStamp: object.timeStamp,
        nonceStr: object.nonceStr,
        package: object.package,
        signType: object.signType,
        paySign: object.paySign,
        success() {
          wx.showToast({
            title: '支付成功',
          })
          that.setData({
            'workItem.State': 2
          })
        },
        fail() {
          console.log('支付失败')
        },
        complete(res) {
          if (res.errMsg === 'requestPayment:fail cancel') {
            wx.showToast({
              icon: 'none',
              title: '用户取消支付'
            })
          }
        }
      })
    }
  }
})
